<?php

/**
 * 学生在籍人数设置
 * 
 * @version 1.0
 * @date 2013-12-12
 * @author wf
 */
class DefaultController extends FController {

    public $user_school_list;
    public $user_subject_list;
    public $user_role_list;
    public $exam_id;
    public function accessRules() {
        $config = array(
            'index' => 1,
            'membershipadd' => 1,
            'membershipedit' => 1,
            'deletemembership' => 1,
            'subordinate' => 1,
            'subordinatedit' => 1,
            'deletesubordinate' => 1
        );
        return ZRole::auth("role", $config);
    }

    public function __common() {
        parent::init();
        if (empty($this->cLoginUser)) {
            $this->redirect($this->createUrl('/site/login'));
        } else {
            $userLoginUser = $this->cLoginUser;
            $this->user_role_list = isset($userLoginUser['cuser']['role_id']) ? $userLoginUser['cuser']['role_id'] : '';
            $this->user_school_list = isset($userLoginUser['cuser']['school']['school_id']) ? $userLoginUser['cuser']['school']['school_id'] : '';
            $this->user_subject_list = isset($userLoginUser['cuser']['school']['subject_id']) ? $userLoginUser['cuser']['school']['subject_id'] : '';
            $zRow = new ZRole();
            //得到学校对应的考试
            $exam_list = $zRow->_getSchoolExam($this->user_school_list);
            if (!empty($exam_list)) {
                $this->exam_id = implode(',', $exam_list);
            }
        }
        $this->commonScript();
        Yii::app()->clientScript->registerCssFile(THEME_CSS_URL . '/module_Sip.css');
    }

    /**
     * @version 1.0
     * @author wf
     * @date 2013-12-12
     */
    public function actionIndex() {
        $this->__common();
        $this->title = "(高中)学生在籍人数设置";
        $s['membership_code'] = (string) $this->request->getParam('membership_code');
        $s['membership_name'] = (string) $this->request->getParam('membership_name');
        $s = ZTask::dis_input($s);
        $s = Func::fAddslashes($s);
        $memebership = ZMembership::_getMembershipAll($s);
        if ($memebership) {
            foreach ($memebership['listDatas'] as $key => $one) {
                $edutype = new EduType();
                $edutypeinfo = $edutype->findByPk($one['edu_type']);
                if ($edutypeinfo) {
                    $memebership['listDatas'][$key]['edutype_name'] = $edutypeinfo->edu_name;
                } else {
                    $memebership['listDatas'][$key]['edutype_name'] = '';
                }
            }
        }
        $pageUrl = $this->createUrl('/membership/default/index', $s);
        $this->render('index', array(
            'memebership' => $memebership['listDatas'],
            'search' => $s,
            'pageUrl' => $pageUrl,
            'pages' => $memebership['pages'],
        ));
    }

    //添加学生在籍人数
    public function actionMembershipadd() {
        $this->__common();
        $membershipinfo = array();
        $membership_id = (int) $this->request->getParam('membership_id');
        if ($membership_id) {
            $membership = new Membership();
            $membershipinfo = $membership->findByPk($membership_id);
            if ($membershipinfo) {
                $membershipinfo = $membershipinfo->attributes;
            }
        }
        $examdate = new ExamData();
        $edutype = $examdate->_getEduType();
        $this->renderPartial("membershipadd", array('edutype' => $edutype, 'membershipinfo' => $membershipinfo), true);
    }

    //学生在籍人数保存 
    public function actionMembershipedit() {
        if ($_POST) {
            $membership = new Membership();
            if (!empty($_POST['membership_id'])) {
                $membership = $membership->findByPk($_POST['membership_id']);
                $membership->membership_code = strtolower($_POST['membership_code']);
                # 验重  #
                if ($membership->membership_code != strtolower($_POST['membership_code'])) {
                    $check = Membership::model()->find(' "membership_code" = \'' . strtolower($_POST['membership_code']) . '\' AND "membership_id" != '. $_POST['membership_id']);
                    if (!empty($check)) {
                    	if ($check->is_del == 1){
                    		$this->frameAjaxMessage(null, "该代码已存在回收站中,不可重复使用!");exit;
                    	}else{
                        	$this->frameAjaxMessage(null, "代码已被占用");exit;
                    	}
                        exit;
                    }
                }
                $membership->membership_name = $_POST['membership_name'];
                $membership->edu_type = $_POST['edu_type'];
                $membership->disabled = empty($_POST['disabled']) ? 0 : $_POST['disabled'];
                $membership->description = $_POST['description'];
                $membership->create_user = $this->cLoginUser['cuser']['id'];
                $membership->create_time = time();
                $membership->update_user = $this->cLoginUser['cuser']['id'];
                $membership->update_time = time();
            } else {

                $membership->membership_code = strtolower($_POST['membership_code']);
                # 验重  #

                $check = Membership::model()->find('"membership_code" = \'' . $membership->membership_code . '\'');
                if (!empty($check)) {
                	if ($check->is_del == 1){
                    		$this->frameAjaxMessage(null, "该代码已存在回收站中,不可重复使用!");
                    	}else{
                        	$this->frameAjaxMessage(null, "代码已被占用");
                    	}
                    exit;
                }

                $membership->membership_name = $_POST['membership_name'];
                $membership->edu_type = $_POST['edu_type'];
                $membership->disabled = empty($_POST['disabled']) ? 0 : $_POST['disabled'];
                $membership->description = empty($_POST['description']) ? '' : $_POST['description'];

                $membership->update_user = $this->cLoginUser['cuser']['id'];
                $membership->update_time = time();
            }
            if ($membership->save()) {
                $this->frameAjaxMessage('/membership/default/index');
            } else {
                echo "参数保存错误 请注意填写";
                exit;
            }
        }
    }

    //删除学生在籍人数
    public function actionDeletemembership() {
        $membership_id = (int) $this->request->getParam('membership_id');
        $membership = new Membership();
        if ($membership->deleteByPk($membership_id)) {
            $membershipsum = new MembershipNum();
            $membershipsum->deleteAll(' "membership_id" = ' . $membership_id);
            echo 1;
        } else {
            echo 2;
        }
    }

    /**
	* @ 批量删除学段
	* @ yindebin
	* @ 2014-01-06 pm
	*
	*/
	public function actionDelete_all(){
		$id = Yii::app ()->request->getparam ( 'id' );
		
		$select = 'SELECT "mn_id" FROM "{{membership_num}}" WHERE "membership_id" IN ('.$id.') AND "is_del" = 0';
   		$data = Yii::app()->db->createCommand($select)->queryRow();
   		if(!empty($data)){
   			echo 3;
   			exit;
   		}
		if (! empty ( $id )) {
			$delete = 'DELETE FROM "{{membership}}" WHERE "membership_id" IN ('.$id.')';
			$query = Yii::app()->db->createCommand($delete)->execute();
			echo 1;
		}else{
			echo 2;
		}
	}		
    										

    //对应 人数
    public function actionSubordinate() {
        $this->__common();
        $title = "学生在籍人数";
        $page = (int) $this->request->getParam('page', 1);
        $membership_id = $s['membership_id'] = (int) $this->request->getParam('membership_id');
        $memebership = array();


        $subject_array = $subject_object = array();

        $sql = 'SELECT "mt_id","mt_name","mt_type","mt_code" FROM "{{membership_type}}" ';
        $result = Yii::app()->db->createCommand($sql);
        $data = $result->queryAll();
        foreach ($data as $value) {
            if ($value['mt_type'] == 1) {
                $subject_array[1][] = $value;
                $subject_object[1][] = $value['mt_id'];
            } else {
                $subject_array[0][] = $value;
                $subject_object[0][] = $value['mt_id'];
            }
        }
        $school_num = array();
        $sql_school = 'SELECT "school_id","mt_id","mt_num" FROM "{{membership_num}}" WHERE "membership_id" = ' . $membership_id . ' ORDER BY "mn_id" ASC ';
        $sResult = Yii::app()->db->createCommand($sql_school);
        $sData = $sResult->queryAll();

        if (!empty($sData)) {
            foreach ($sData as $val) {
                if ($val['school_id']) {
                    $school = new School();
                    $school_info = $school->findByPk($val['school_id']);
                    if ($school_info) {
                        $school_info = $school_info->attributes;
                        $school_num[$val['school_id']]['school_name'] = $school_info['school_name'];
                    } else {
                        $school_num[$val['school_id']]['school_name'] = '';
                    }
                }
                if (in_array($val['mt_id'], $subject_object[1])) {
                    $school_num[$val['school_id']][1][$val['mt_id']] = $val['mt_num'];
                } else {
                    $school_num[$val['school_id']][0][$val['mt_id']] = $val['mt_num'];
                }
            }
            if (isset($school_num[$val['school_id']][1])) {
                ksort($school_num[$val['school_id']][1]);
            }
            if (isset($school_num[$val['school_id']][0])) {
                ksort($school_num[$val['school_id']][0]);
            }
        }
        $pageUrl = $this->createUrl('/membership/default/index', $s);
        $this->render('subordinate', array(
            'memebership' => $memebership,
            'title' => $title,
            'pageUrl' => $pageUrl,
            'page' => $page,
            'membership_id' => $membership_id,
            'subject_array' => $subject_array,
            'subject_object' => $subject_object,
            'school_num' => $school_num
        ));
    }

    //添加学生在籍人数
    public function actionSubordinateadd() {
        $this->__common();
        $membershipnuminfo = array();
        $membership_id = (int) $this->request->getParam('membership_id');
        $school_id = (int) $this->request->getParam('school_id');
        if ($school_id) {
            $membershipnuminfo = ZMembership::_getAllmembershiptypenum('"school_id" = ' . $school_id . ' AND "membership_id" =' . $membership_id);
        }
        
        $zEdu = new ZEdu();
        if ($this->user_role_list != 1 && !empty($this->user_school_list)) {
            $school = $zEdu->_getSchoolInfo($this->user_school_list);
        } else {
            $school = $zEdu->_getSchoolInfo();
        }
        $membershiptype = ZMembership::_getAllmembershiptype();
        $this->renderPartial("subordinateadd", array('school' => $school, 'membershiptype' => $membershiptype, 'membershipnuminfo' => $membershipnuminfo, 'membership_id' => $membership_id, 'school_id' => $school_id), true);
    }

    //学生在籍人数保存 
    public function actionSubordinatedit() {
        if ($_POST) {
   		$membershipnum = new MembershipNum();
	
		if(isset($_POST['is_update']) && !empty($_POST['is_update'])){
			$check = $membershipnum->model()->count(' "school_id" = '.$_POST['school_id'].' AND "membership_id" = '.$_POST['membership_id'].' AND "school_id" != '.$_POST['is_update']);
		}else{
			$check = $membershipnum->model()->count(' "school_id" = '.$_POST['school_id'].' AND "membership_id" = '.$_POST['membership_id']);
		}
   		if($check > 0){
   			$this->frameAjaxMessage(null,'已存在该学校数据!');
			exit;
   		}
            if ($_POST['mn_id']) {
                foreach ($_POST['mn_id'] as $key => $one) {
                    $membershipnum = new MembershipNum();
                    $membershipnum = $membershipnum->findByPk($one);
                    $membershipnum->school_id = $_POST['school_id'];
                    $membershipnum->mt_id = $_POST['mt_id'][$key];
                    $membershipnum->mt_num = (!empty($_POST['mt_num'][$key])) ? $_POST['mt_num'][$key] : 0;
                    $membershipnum->membership_id = $_POST['membership_id'];
                    $membershipnum->save();
                }
                $this->frameAjaxMessage('/membership/default/subordinate/membership_id/' . $_POST['membership_id']);
            } else {
                foreach ($_POST['mt_id'] as $key => $one) {
                    $membershipnum = new MembershipNum();
                    $membershipnum->school_id = $_POST['school_id'];
                    $membershipnum->mt_id = $one;
                    $membershipnum->mt_num = (!empty($_POST['mt_num'][$key])) ? $_POST['mt_num'][$key] : 0;
                    $membershipnum->membership_id = $_POST['membership_id'];
                    $membershipnum->save();
                }
                $this->frameAjaxMessage('/membership/default/subordinate/membership_id/' . $_POST['membership_id']);
            }
        }
    }

    //删除学生在籍人数
    public function actionDeletesubordinate() {
        $school_id = (int) $this->request->getParam('school_id');
        $membership_id = (int) $this->request->getParam('membership_id');
        $membershipnum = new MembershipNum();
        if ($membershipnum->deleteAll('"school_id" =' . $school_id . ' AND "membership_id" = ' . $membership_id)) {
            echo 1;
        } else {
            echo 2;
        }
    }

}